package clusterapi

import (
	"fmt"
	"time"
)

// Config is the configuration for a cluster manager
type Config struct {
	// NodeID is the ID of the node
	NodeID string
	// BindAddress is the address to bind to
	BindAddress string
	// AdvertiseAddress is the address to advertise
	AdvertiseAddress string
	// SeedAddresses are the addresses of seed nodes
	SeedAddresses []string
	// Role is the role of the node
	Role string
	// Tags are the tags of the node
	Tags []string
	// Metadata is the metadata of the node
	Metadata map[string]string
	// HeartbeatInterval is the interval between heartbeats
	HeartbeatInterval time.Duration
	// HeartbeatTimeout is the timeout for heartbeats
	HeartbeatTimeout time.Duration
}

// DefaultConfig returns the default configuration
func DefaultConfig() *Config {
	return &Config{
		BindAddress:       "0.0.0.0:7946",
		AdvertiseAddress:  "",
		SeedAddresses:     []string{},
		Role:              "worker",
		Tags:              []string{},
		Metadata:          map[string]string{},
		HeartbeatInterval: 1 * time.Second,
		HeartbeatTimeout:  3 * time.Second,
	}
}

// Validate validates the configuration
func (c *Config) Validate() error {
	if c.BindAddress == "" {
		return fmt.Errorf("bind address is required")
	}

	if c.AdvertiseAddress == "" {
		c.AdvertiseAddress = c.BindAddress
	}

	return nil
}
